#pragma once

#include <iostream>
#include <string>
#include "Common.hpp"
#include "Log.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <functional>
#include <cerrno>
#include <cstring>
#include <cstdlib>
using namespace LogModule;
using func_t = std::function<std::string(const std::string &, struct sockaddr &)>;
class TcpServer : public NoCopy
{
public:
    TcpServer(uint16_t port)
        : _port(port),
          _isrunning(false)

    {
    }
    TcpServer(uint16_t port, func_t func)
        : _port(port),
          _isrunning(false),
          _func(func)
    {
    }
    ~TcpServer() = default;
    void Init()
    {
        // 1.socket
        _listensock = socket(AF_INET, SOCK_STREAM, 0);
        if (_listensock < 0)
        {
            LOG(LogLevel::FATAL) << "socket error";
            exit(SOCKET_ERR);
        }
        LOG(LogLevel::INFO) << "socket success";
        int opt = 1;
        setsockopt(_listensock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); // 单独设置SO_REUSEADDR
        setsockopt(_listensock, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)); // 单独设置SO_REUSEPORT
        // 2.bind
        struct sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = htonl(INADDR_ANY); // host to net 主机转网络
        int n = bind(_listensock, (const sockaddr *)&local, sizeof(local));
        if (n < 0)
        {
            LOG(LogLevel::FATAL) << "bind error";
            exit(BIND_ERR);
        }
        LOG(LogLevel::INFO) << "bind success";
        // 3. listen
        n = listen(_listensock, 5);
        if (n != 0)
        {
            LOG(LogLevel::FATAL) << "listen error";
            exit(LISTEN_ERR);
        }
        LOG(LogLevel::INFO) << "listen success";
    }
    void Service(int sockfd)
    {
        char buffer[1024];
        while (1)
        {
            ssize_t n = read(sockfd, buffer, sizeof(buffer) - 1);
            if (n > 0)
            {
                LOG(LogLevel::INFO) << "read success";
                buffer[n] = 0;
                std::cout << "client say# " << buffer << std::endl;
                std::string echo_string = "server echo# ";
                echo_string += buffer;
                write(sockfd, echo_string.c_str(), echo_string.size());
            }
            else if (n == 0)
            {
                LOG(LogLevel::INFO) << "read quit";
                break;
            }
            else
            {
                LOG(LogLevel::FATAL) << "read2 error";
                break;
            }
        }
    }
    void Start()
    {
        _isrunning = true;
        while (_isrunning)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sockfd = accept(_listensock, (sockaddr *)&peer, &len);
            if (sockfd < 0)
            {
                // 日志
                LOG(LogLevel::FATAL) << "accept error";
                continue;
            }
            LOG(LogLevel::INFO) << "accept success";
            // 日志
            Service(sockfd);
            close(sockfd);
        }
    }

private:
    uint16_t _port;
    int _listensock;
    bool _isrunning;
    func_t _func;
};